package com.kungfuhacking.wristbandpro.location.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.InputView;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.location.presenter.ISNCodePresenter;
import com.kungfuhacking.wristbandpro.location.view.ISNCodeView;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

/**
 * 添加sn码页面
 * Created by Aaron on 17/5/9.
 */
public class AddSNCodeActivity extends BaseActivity implements ISNCodeView, View.OnClickListener {

    private ISNCodePresenter isnCodePresenter = new ISNCodePresenter(this);
    private com.kungfuhacking.wristbandpro.custom.TitleBarView tbv;
    private com.kungfuhacking.wristbandpro.custom.InputView ivAccount;
    private final int REQUEST_CODE = 0x00211;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sn_code);
        TextView tvDone = (TextView) findViewById(R.id.tvDone);
        this.ivAccount = (InputView) findViewById(R.id.ivAccount);
        this.tbv = (TitleBarView) findViewById(R.id.tbv);

        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvDone.setOnClickListener(this);
        tbv.setOnRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextActivityWithCode(CaptureActivity.class, REQUEST_CODE);
            }
        });//扫描
    }

    @Override
    public String getSNCode() {
        return ivAccount.getText();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvDone://完成

                if (TextUtils.isEmpty(ivAccount.getText())) {
                    showErr("请输入手表IMET码!");
                    return;
                }

                isnCodePresenter.bindByIEMI(ivAccount.getText());

                break;

        }
    }

    private String TAG = getClass().getSimpleName();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE) {
            if (data == null)
                return;

            Bundle bundle = data.getExtras();
            if (bundle == null) {
                return;
            }
            if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                String result = bundle.getString(CodeUtils.RESULT_STRING);
                if (!TextUtils.isEmpty(result))
                    isnCodePresenter.bindByScan(result);
                Log.d(TAG, "解析结果:" + result);
            } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                showErr("解析二维码失败");
            }

        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        isnCodePresenter.clearRequest();
    }
}
